package fnic.prehand.esagent.passitive_measure;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import fnic.prehand.augment.Flow;

public class Aggregate {
	Map<AggrKey, Flow> aggrmap = new HashMap<AggrKey, Flow>();

	public void push(Flow flow){
		AggrKey key = getKey(flow);
		if(aggrmap.containsKey(key)){
			aggrvalue(key, flow);
		}else{
			aggrmap.put(key, flow);
		}
	}
	
	private void aggrvalue(AggrKey key, Flow append){
		long[] target = aggrmap.get(key).num_oldFlow;
		target[7] += append.num_oldFlow[7];
		target[8] += append.num_oldFlow[8];
		target[9] += append.num_oldFlow[9];
		target[10] += append.num_oldFlow[10];
	}
	
	//根据六元组来汇聚原始流数据
	private AggrKey getKey(Flow f){
		long [] tKey = new long []{f.num_oldFlow[1],    //linkID
			                            f.num_oldFlow[2],  //SourceIP
			                            f.num_oldFlow[3], //DestionIp
			                            f.num_oldFlow[4],  //SourceIP
			                            f.num_oldFlow[5], //SourceIP
			                            f.num_oldFlow[6]};  //SourceIP
		AggrKey k = new AggrKey(tKey);
		return k;
	}
	
	public Iterator<Flow> getAggrMapIterator() {
		return aggrmap.values().iterator();
	}
	
	static class AggrKey{
		public long [] key;
	     
	     public AggrKey(long [] key) {
	    	 this.key = key;
	     }
	     
	     @Override
	    public int hashCode() {
	    	 return Arrays.hashCode(this.key);
	    }
	    
	    @Override
	    public boolean equals(Object obj) {
	    	if(obj == null)
	    		return false;
	    	if(obj == this)
	    		return true;
	    	return Arrays.hashCode(this.key) == Arrays.hashCode(((AggrKey)obj).key);
	    }
	    
	     
	    public static void main(String[] args) {
	    	 Map<AggrKey, long []> map = new HashMap<AggrKey, long[]>();
	    	 AggrKey k1 = new AggrKey(new long[]{2,3,4});
	    	 AggrKey k2 = new AggrKey(new long[]{2,3,4});
	    	 map.put(k1, new long[]{1,3});
	    	 System.out.println(map.get(k2));
	    	 System.out.println(map.get(k1));
	    	 
	     }
	}
}
